package world.criver.mm_vworld_server.function;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import world.criver.crc.CRC;
import world.criver.mm_vworld_server.config.conConfig;
import world.criver.mm_vworld_server.data.daSave;
import world.criver.mm_vworld_server.modular.moPlayer;
import world.criver.mm_vworld_server.modular.moSaveAvatar;
import world.criver.mm_vworld_server.modular.moSaveDateTime;
import world.criver.mm_vworld_server.modular.moSaveProperties;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Component
public class funSave {
    @Autowired
    private conConfig conConfig;
    @Autowired
    private funFile funFile;

    private String path_properties="/properties.mm";
    private String path_dateTime="/dateTime.mm";
    private String path_avatar="/avatar.mm";
    private String path_players="/players";

    private ObjectMapper om=new ObjectMapper();

    /**
     * 加载存档
     * @return
     */
    public boolean load(){
        try{
            File f=new File(conConfig.getSavePath());
            if(!f.exists())f.mkdirs();
            String da="";
            //配置文件
            File fpro=new File(conConfig.getSavePath()+path_properties);
            if(fpro.exists()){
                da= funFile.readFileOfMM(fpro);
                if(da!=null)daSave.properties=om.treeToValue(om.readTree(da), moSaveProperties.class);
            }
            //存档日期
            File fdt=new File(conConfig.getSavePath()+path_dateTime);
            if(fdt.exists()){
                da= funFile.readFileOfMM(fdt);
                if(da!=null)daSave.dateTime=om.treeToValue(om.readTree(da), moSaveDateTime.class);
            }
            //存档头像
            File fa=new File(conConfig.getSavePath()+path_avatar);
            if(fa.exists()){
                da= funFile.readFileOfMM(fa);
                if(da!=null)daSave.avatar=om.treeToValue(om.readTree(da), moSaveAvatar.class);
            }
            //玩家数据
            File fff=new File(conConfig.getSavePath()+path_players);
            if(!fff.exists())fff.mkdirs();
            File[] fps=fff.listFiles();
            for(File fp:fps){
                da= funFile.readFileOfMM(fp);
                if(da!=null){
                    moPlayer player=om.treeToValue(om.readTree(da), moPlayer.class);
                    daSave.players.put(player.getNickname(),player);
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 保存存档
     * @return
     */
    public boolean save(){
        try{
            File f=new File(conConfig.getSavePath());
            if(!f.exists())f.mkdirs();
            om.enable(SerializationFeature.INDENT_OUTPUT);
            funFile.writeFileOfMM(om.valueToTree(daSave.properties).toString(),new File(conConfig.getSavePath()+path_properties));
            funFile.writeFileOfMM(om.valueToTree(daSave.dateTime).toString(),new File(conConfig.getSavePath()+path_dateTime));
            funFile.writeFileOfMM(om.valueToTree(daSave.avatar).toString(),new File(conConfig.getSavePath()+path_avatar));
            for(moPlayer mo:daSave.players.values())
                funFile.writeFileOfMM(om.valueToTree(mo).toString(),new File(conConfig.getSavePath()+path_players+"/"+ Base64.getEncoder().encodeToString(mo.getNickname().getBytes(StandardCharsets.UTF_8))+".mm"));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

}
